package com.muki.simple;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.muki.simple.configuration.InterestConf;
import com.muki.simple.core.SpiderProcessor;
import com.muki.simple.repository.ContentPipeline;

import us.codecraft.webmagic.Spider;

@SpringBootApplication
@EnableScheduling
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SpiderApplication {

	@Autowired
	private ContentPipeline contentPipeline;
	
	@Autowired
	private InterestConf interest;
	
	@Scheduled(fixedDelay=10*1000*60*60)
	public void execute() {
		Spider spider = Spider.create(new SpiderProcessor(interest));
		for(String root:interest.getRoot()) {
			spider.addUrl(root);
		}
		spider.addPipeline(contentPipeline);
		spider.thread(interest.getNum())
		.setExitWhenComplete(true)
		.run();
	}

	public static void main(String[] args) {
		SpringApplication.run(SpiderApplication.class, args);
	}

}
